package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.entity.ChartData;
import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.utils.EchartsUtils;
import cn.zysuyuan.item.entity.DeviceData;
import cn.zysuyuan.item.mapper.DeviceDataMapper;
import cn.zysuyuan.item.service.DeviceDataService;
import cn.zysuyuan.item.utils.CaculateChartData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2019/10/28 14:07
 */

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResult<DeviceData> selectDeviceDataByPage(Map<String,Object> map) {

        // 将查询到的结果保存在DeviceData类型的list中
        List<DeviceData> deviceData = this.deviceDataMapper.selectList(map);
        // 包装成分页的结果集返回
        return new PageResult<DeviceData>(deviceData);
    }

    @Override
    public int saveInfo(DeviceData sensorDevices) {

        return this.deviceDataMapper.insertDeviceData(sensorDevices);
    }

    @Override
    public List<DeviceData> selectNewOne() {
        List<DeviceData> list = this.deviceDataMapper.selectNewOne();
        return list;
    }

    /**
     * 此方法已废弃不用，一堆屎山留作纪念，嘿嘿
     * @param name
     * @return
     */
    @Override
    public Map<String, Object> selectEachDeviceData(String name) {
        List<DeviceData> list = deviceDataMapper.selectEachDeviceData();
        Map<String,Object> maps = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap<String, Object>();
        List<ChartData> newList = new ArrayList<ChartData>();
        List<Double> datas = new ArrayList<Double>();
        List<List<Double>> values = new ArrayList<List<Double>>(12);
        List<Double> zero = new  ArrayList<Double>();
        List<Double> two = new  ArrayList<Double>();
        List<Double> four = new  ArrayList<Double>();
        List<Double> six = new  ArrayList<Double>();
        List<Double> eight = new  ArrayList<Double>();
        List<Double> ten = new  ArrayList<Double>();
        List<Double> twelve = new  ArrayList<Double>();
        List<Double> fourteen = new  ArrayList<Double>();
        List<Double> sixteen = new  ArrayList<Double>();
        List<Double> eighteen = new  ArrayList<Double>();
        List<Double> twenty = new  ArrayList<Double>();
        List<Double> twentytwo = new  ArrayList<Double>();
        for (int i = 0;i < list.size(); i++) {
            ChartData chartData = new ChartData();
            CaculateChartData.JudgeDevice(name,chartData,list,i);
            try {
                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
                String date = df.format(chartData.getCreateTime());
                Date time = df.parse(date);
                Calendar nowTime = Calendar.getInstance();
                nowTime.setTime(time);
                if (nowTime.after(dateLine("00:00:00")) && nowTime.before(dateLine("01:00:00"))) {
                    zero.add(chartData.getData());
                    System.out.println(i + ":" + "在0-1点之间");
                }
                else if (nowTime.after(dateLine("02:00:00")) && nowTime.before(dateLine("03:00:00"))) {
                    two.add(chartData.getData());
                    System.out.println(i + ":" + "在2-3点之间");
                }
                else if (nowTime.after(dateLine("04:00:00")) && nowTime.before(dateLine("05:00:00"))) {
                    four.add(chartData.getData());
                    System.out.println(i + ":" + "在4-5点之间");
                }
                else if (nowTime.after(dateLine("06:00:00")) && nowTime.before(dateLine("07:00:00"))) {
                    six.add(chartData.getData());
                    System.out.println(i + ":" + "在6-7点之间");
                }
                else if (nowTime.after(dateLine("08:00:00")) && nowTime.before(dateLine("09:00:00"))) {
                    eight.add(chartData.getData());
                    System.out.println(i + ":" + "在8-9点之间");
                }
                else if (nowTime.after(dateLine("10:00:00")) && nowTime.before(dateLine("11:00:00"))) {
                    ten.add(chartData.getData());
                    System.out.println(i + ":" + "在10-11点之间");
                }
                else if (nowTime.after(dateLine("12:00:00")) && nowTime.before(dateLine("13:00:00"))) {
                    twelve.add(chartData.getData());
                    System.out.println(i + ":" + "在12-13点之间");
                }
                else if (nowTime.after(dateLine("14:00:00")) && nowTime.before(dateLine("15:00:00"))) {
                    fourteen.add(chartData.getData());
                    System.out.println(i + ":" + "在14-15点之间");
                }
                else if (nowTime.after(dateLine("16:00:00")) && nowTime.before(dateLine("17:00:00"))) {
                    sixteen.add(chartData.getData());
                    System.out.println(i + ":" + "在16-17点之间");
                }
                else if (nowTime.after(dateLine("18:00:00")) && nowTime.before(dateLine("19:00:00"))) {
                    eighteen.add(chartData.getData());
                    System.out.println(i + ":" + "在18-19点之间");
                }
                else if (nowTime.after(dateLine("20:00:00")) && nowTime.before(dateLine("21:00:00"))) {
                    twenty.add(chartData.getData());
                    System.out.println(i + ":" + "在20-21点之间");
                }
                else if (nowTime.after(dateLine("22:00:00")) && nowTime.before(dateLine("23:00:00"))) {
                    twentytwo.add(chartData.getData());
                    System.out.println(i + ":" + "在22-23点之间");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            newList.add(chartData);
        }
        values.add(zero);
        values.add(two);
        values.add(four);
        values.add(six);
        values.add(eight);
        values.add(ten);
        values.add(twelve);
        values.add(fourteen);
        values.add(sixteen);
        values.add(eighteen);
        values.add(twenty);
        values.add(twentytwo);
        for (int j = 0; j < values.size(); j++) {
            datas.add(CaculateAverage(values.get(j)));
        }
        System.out.println("数据：" + datas);

        map.put("DayData",datas);
//        maps.put(name,map);
        return map;
    }

    /**
     * 将上面啰嗦的if else进行算法封装优化
     * @param name
     * @return
     */
    @Override
    public Map<String, Object> getEchartsData(String name) {
        long startTime=System.currentTimeMillis();   //获取开始时间
        List<DeviceData> list = deviceDataMapper.selectEachDeviceData();
        Map<String,Object> map = new HashMap<>();
        List<Double> datas = new ArrayList<>();
        // 获取当前时间的小时
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        try {
            datas = EchartsUtils.getEchartsData(gregorianCalendar.get(Calendar.HOUR_OF_DAY),1,list,name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间： "+(endTime-startTime)+"ms");
        map.put("DayData",datas);
        return map;
    }

    @Override
    public int deleteById(String id) {
        return deviceDataMapper.deleteById(id);
    }

    public static Calendar dateLine(String date){
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        Calendar time = null;
        try {
            Date ctime = df.parse(date);
            time = Calendar.getInstance();
            time.setTime(ctime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static Double CaculateAverage(List<Double> list) {
        Double sum = 0.0;
        int num = list.size();
        for (int i = 0; i < list.size(); i++) {
            sum = sum + list.get(i);
        }
        System.out.println(sum);
        if (list.size() == 0) {
            return null;
        }else {
            return sum/num;
        }
    }


}
